package com.wlyuan.gateway;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author yuanjie
 */
@Slf4j
@EnableOpenApi
@Configuration
@ConditionalOnProperty(prefix = "document", name = "enabled")
public class DocumentConfiguration {
    @Value("${document.title:}")
    private String title;
    @Value("${document.description:}")
    private String description;
    @Value("${document.contactName:}")
    private String contactName;
    @Value("${document.contactUrl:}")
    private String contactUrl;
    @Value("${document.contactEmail:}")
    private String contactEmail;

    @Bean
    public Docket createRestApi() {
        logger.info("创建Swagger文档支持");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .description(description)
                .title(title).build();


        // 创建API基本信息
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }
}
